﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MouseExtender.Logic;
using MouseExtender.Logic.Managers;
using System.Diagnostics;
using System.Windows.Media.Animation;

namespace MouseExtender.Controls.UserControls
{
    /// <summary>
    /// Interaction logic for UpdateCheckBalloonUC.xaml
    /// </summary>
    public partial class UpdateCheckBalloonUC : UserControl
    {
        public UpdateCheckBalloonUC()
        {
            InitializeComponent();
        }

        private void UpdateUI()
        {
            UpdateManager.UpdateInfo state = this.DataContext as UpdateManager.UpdateInfo;
            if (state == null)
                return;

            if (state.Status >= UpdateManager.UpdateStatus.NewVersionAvailable)
            {
                tbDownloadUrl.Visibility = Visibility.Visible;
            }
            if (state.Status == UpdateManager.UpdateStatus.UpdateComleted)
            {
                hlReleaseDetails.NavigateUri = new Uri(UpdateManager.defaultReleaseUrl, UriKind.Absolute);
            }
            if (state.Status == UpdateManager.UpdateStatus.UpdatesDownloaded)
            {
                tbInstall.Visibility = Visibility.Visible;
            }
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Storyboard fadeToBack = (Storyboard)this.Resources["sbFadeToBack"];
            if (fadeToBack != null)
                fadeToBack.Begin();
        }

        private void hlReleaseDetails_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo(e.Uri.ToString());
            Process.Start(startInfo);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateUI();
        }

        private void hlInstall_Click(object sender, RoutedEventArgs e)
        {
            UpdateManager.InstallUpdatesAsync();
        }
    }
}
